package cn.cs.service.impl;

import cn.cs.client.AgentProductOrderClient;
import cn.cs.client.AgentUserForShopDB;
import cn.cs.client.AgentUserForUserDB;
import cn.cs.client.SysDtAreaClient;
import cn.cs.common.*;
import cn.cs.service.AgentProductOrderService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class AgentProductOrderServiceImpl implements AgentProductOrderService {
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AgentUserForUserDB agentUserForUserDB;
    @Resource
    private AgentUserForShopDB agentUserForShopDB;
    @Resource
    private AgentProductOrderClient agentProductOrderClient;
    @Resource
    private SysDtAreaClient sysDtAreaClient;
    @Override
    public Dto getPlatProList(HttpServletRequest request) {
        String type= request.getParameter("type");
        String name = request.getParameter("name");
        String pageNum = request.getParameter("pageNum");
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("begNum",(Integer.valueOf(pageNum)-1)*10);
        param.put("endNum",Integer.valueOf(pageNum)*10);
        param.put("type",type);
        if(EmptyUtils.isNotEmpty(name)){
            param.put("name","%"+name+"%");
        }
        List<Map<String,Object>> list = agentUserForUserDB.getPlatProList(param);
        if(EmptyUtils.isNotEmpty(list)){
            return  DtoUtil.returnDataSuccess(list);
        }else {
            return DtoUtil.returnFail("没有找到产品","002");
        }
    }

    @Override
    public Dto platProInfo(HttpServletRequest request) {
        String id  = request.getParameter("id");
        Map<String,Object> plantPro = agentUserForUserDB.getPlatInfo(id);
        Map<String,Object>   proType= agentUserForShopDB.getProTypeById(plantPro.get("parts_type").toString());
        plantPro.put("type_name",proType);
        if(EmptyUtils.isNotEmpty(plantPro)){
            List<Map<String,Object>> list = agentUserForUserDB.getProImageList(id);
            List<Map<String,Object>> details = new ArrayList<Map<String, Object>>();
            List<Map<String,Object>> rotary = new ArrayList<Map<String, Object>>();
            for(Map<String,Object> m :list){
                if("1".equals(m.get("type").toString())){
                    details.add(m);
                }else{
                    rotary.add(m);
                }
            }
            plantPro.put("lbImg",rotary);
            plantPro.put("dtImg",details);
            plantPro.put("imgs",list);
            return DtoUtil.returnDataSuccess(plantPro);
        }else{
            return DtoUtil.returnFail("没有找到此商品","002");
        }
    }

    @Override
    public Dto agentPlaceOrder(HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        String proId = request.getParameter("id");
        String count = request.getParameter("count");
        String address = request.getParameter("address");
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id", IdWorker.getId());
        param.put("agent_id",agentUser.get("id").toString());
        param.put("parts_count",count);
        param.put("address",address);
        param.put("parts_id",proId);
        param.put("state","0");
        param.put("create_time", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        int n = agentProductOrderClient.createAgentPlaceOrder(param);
        if(n>0){
            return DtoUtil.returnSuccess();
        }else{
            return DtoUtil.returnFail("生成失败","002");
        }
    }

    @Override
    public Dto getPlaceOrderList(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        List<Map<String,Object>> list = agentProductOrderClient.getPlaceOrderList(agentUser.get("id").toString());
        for(Map<String,Object> order : list){
            Map<String,Object> partInfo = agentUserForUserDB.getCsPartInfoById(order.get("parts_id").toString());
            order.put("image_url",partInfo.get("image_url").toString());
            order.put("parts_name",partInfo.get("parts_name").toString());
            order.put("parts_details",partInfo.get("parts_details").toString());
        }
        String day =  agentUserForUserDB.getCancelDay();
        Map<String,Object> map =new HashMap<String, Object>();
        map.put("list",list);
        map.put("day",day);
        if(EmptyUtils.isNotEmpty(list)){
            return  DtoUtil.returnDataSuccess(map);
        }else{
            return  DtoUtil.returnFail("没有订单列表","002");
        }
    }

    @Override
    public Dto canclePlaceOrder(HttpServletRequest request) throws Exception{
        String id = request.getParameter("id");
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("id",id);
        param.put("state","1");
        param.put("update_time",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        int n = agentProductOrderClient.updateAgentPlaceOrder(param);
        if(n>0){
            return  DtoUtil.returnSuccess();
        }else{
            return  DtoUtil.returnFail("取消失败","002");
        }
    }
    @Override
    public Dto agentOrderDetail(HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        String id  = request.getParameter("id");
        Map<String,Object> order = agentProductOrderClient.getOrderInfo(id);
        Map<String,Object> partInfo = agentUserForUserDB.getCsPartInfoById(order.get("parts_id").toString());
        order.put("name",agentUser.get("name"));
        order.put("phone",agentUser.get("phone"));
        order.put("id",agentUser.get("id"));
        order.put("proid",partInfo.get("id"));
        order.put("image_url",partInfo.get("image_url"));
        order.put("parts_name",partInfo.get("parts_name"));
        order.put("parts_price",partInfo.get("parts_price"));
        order.put("parts_details",partInfo.get("parts_details"));
        order.put("address",getPCareaContent(agentUser.get("area").toString())+agentUser.get("address"));
        ReadparameterUtils readparameterUtils = ReadparameterUtils.getInstance();
        order.put("day",readparameterUtils.getVal("cancel_day"));
        if(EmptyUtils.isNotEmpty(order)){
            return  DtoUtil.returnDataSuccess(order);
        }else{
            return DtoUtil.returnFail("没有找到此订单","002");
        }
    }

    public String getPCareaContent(String areaId) throws Exception{
        Map<String,Object> areaf = sysDtAreaClient.getSysDtAreaById(areaId);
        Map<String,Object> areas = sysDtAreaClient.getSysDtAreaById(areaf.get("area_parent_id").toString());
        Map<String,Object> areat = sysDtAreaClient.getSysDtAreaById(areas.get("area_parent_id").toString());
        return  areat.get("area_name")+" "+areas.get("area_name")+" "+areaf.get("area_name");
    }
}
